package com.xiong.xtools.twolist;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xiong.xtools.R;
/**
 * Author : 熊文强
 * Time : 2017/9/19
 * Content : 双列ListView选择控件
 */
public class TwoListViewActivity extends AppCompatActivity {
	ListView leftListview;
	PinnedHeaderListView pinnedListView;
	private boolean isScroll = true;
	private LeftListAdapter adapter;

	private String[] leftStr = new String[]{"面食类", "盖饭", "寿司", "烧烤", "酒水", "凉菜", "小吃", "粥", "休闲"};
	private boolean[] flagArray = {true, false, false, false, false, false, false, false, false};
	private String[][] rightStr = new String[][]{
			{"热干面", "臊子面", "烩面"},
			{"番茄鸡蛋", "红烧排骨", "农家小炒肉"},
			{"芝士", "丑小丫", "金枪鱼"},
			{"羊肉串", "烤鸡翅", "烤羊排"},
			{"长城干红", "燕京鲜啤", "青岛鲜啤"},
			{"拌粉丝", "大拌菜", "菠菜花生"},
			{"小食组", "紫薯"},
			{"小米粥", "大米粥", "南瓜粥", "玉米粥", "紫米粥"},
			{"儿童小汽车", "悠悠球", "熊大", " 熊二", "光头强"}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two_list_view);

		leftListview = (ListView) findViewById(R.id.left_listview);
		pinnedListView = (PinnedHeaderListView) findViewById(R.id.pinnedListView);

		final MainSectionedAdapter sectionedAdapter = new MainSectionedAdapter(this, leftStr, rightStr);
		pinnedListView.setAdapter(sectionedAdapter);
		adapter = new LeftListAdapter(this, leftStr, flagArray);
		leftListview.setAdapter(adapter);
		leftListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View view, int position, long arg3) {
				isScroll = false;

				for (int i = 0; i < leftStr.length; i++) {
					if (i == position) {
						flagArray[i] = true;
					} else {
						flagArray[i] = false;
					}
				}
				adapter.notifyDataSetChanged();
				int rightSection = 0;
				for (int i = 0; i < position; i++) {
					rightSection += sectionedAdapter.getCountForSection(i) + 1;
				}
				pinnedListView.setSelection(rightSection);

			}

		});

		pinnedListView.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView arg0, int scrollState) {
				switch (scrollState) {
					// 当不滚动时
					case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
						// 判断滚动到底部
						if (pinnedListView.getLastVisiblePosition() == (pinnedListView.getCount() - 1)) {
							leftListview.setSelection(ListView.FOCUS_DOWN);
						}

						// 判断滚动到顶部
						if (pinnedListView.getFirstVisiblePosition() == 0) {
							leftListview.setSelection(0);
						}

						break;
				}
			}

			int y = 0;
			int x = 0;
			int z = 0;

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if (isScroll) {
					for (int i = 0; i < rightStr.length; i++) {
						if (i == sectionedAdapter.getSectionForPosition(pinnedListView.getFirstVisiblePosition())) {
							flagArray[i] = true;
							x = i;
						} else {
							flagArray[i] = false;
						}
					}
					if (x != y) {
						adapter.notifyDataSetChanged();
						y = x;
						//左侧ListView滚动到最后位置
						if (y == leftListview.getLastVisiblePosition()) {
							leftListview.setSelection(z);
						}
						//左侧ListView滚动到第一个位置
						if (x == leftListview.getFirstVisiblePosition()) {
							leftListview.setSelection(z);
						}
						if (firstVisibleItem + visibleItemCount == totalItemCount - 1) {
							leftListview.setSelection(ListView.FOCUS_DOWN);
						}
					}
				} else {
					isScroll = true;
				}
			}
		});

	}
}
